<?

class myFacebook {
	
	protected $facebook = NULL;
	protected $page_id = NULL;
	protected $limit_pages = 10;
	protected $limit_users = 30;
	protected $months = 4;
	
	function __construct(){

		require ROOT_PATH . 'src/schnittstellen/facebook/src/facebook.php';
		
		// Create our Application instance (replace this with your appId and secret).
		$this->facebook = new Facebook(array(
		  'appId'  => '420201244679246',
		  'secret' => 'cedd9eb7b741303410e603560dc29099',
		));
			
	}	
	
	function set_page_id($page_id = NULL){
		$this->page_id = $page_id;
	}
	
	
	function get_posts($url = NULL, $number = 0){
		global $db;

				
		$monat = mktime(0,0,0, date('m'), 1, date('Y') );
		$monat_ab = mktime(0,0,0, date('m', time() - 86400*28*$this->months), 1, date('Y') );

		
		if($url == NULL)
			unset($this->all_posts);
		
  		try {
    		// Proceed knowing you have a logged in user who's authenticated.
	    	if($url == NULL)
				$url = $this->page_id . '/posts';
			
			
			$posts = $db->get_data('fb', $url);
			
			if($posts == false){
				$posts = $this->facebook->api($url);
				$db->save_data('fb', $url, $posts);
			}
			
 		 } catch (FacebookApiException $e) {
    		error_log($e);
			print_r($e);
 		}
		
		foreach($posts['data'] as $row){
						
			$date = date_parse($row['created_time']);
			
			$tstamp = mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year']);
								
			if($tstamp <= $monat_ab){
				$number = 1000;
				break;
			}			
						
			$this->all_posts[] = array(
				'tstamp' => mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year']),
				'likes_count' => $row['likes']['count'],
				'comments_count' => $row['comments']['count'],
				'shares_count' => $row['shares']['count'],
				'id' =>  $row['id'],
				);
		}
				
		if(strlen($posts['paging']['next']) > 0 && $number < $this->limit_pages){
			$this->get_posts(str_replace('https://graph.facebook.com/', '', $posts['paging']['next']), $number+1);
		}
		
		return $this->all_posts;
	}

	function get_feed($url = NULL, $number = 0){
		global $db;
		
		if($url == NULL)
			unset($this->all_feeds);
		
  		try {
    		// Proceed knowing you have a logged in user who's authenticated.
	    	if($url == NULL)
				$url = $this->page_id . '/feed';
			
			
			$feeds = $db->get_data('fb', $url);
			
			if($feeds == false){
				$feeds = $this->facebook->api($url);
				$db->save_data('fb', $url, $feeds);
			}
			
 		 } catch (FacebookApiException $e) {
    		error_log($e);
			print_r($e);
 		}
		
		foreach($feeds['data'] as $row){
						
			$date = date_parse($row['created_time']);
			
			$this->all_feeds[] = array(
				'tstamp' => mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year']),
				'likes_count' => $row['likes']['count'],
				'comments_count' => $row['comments']['count'],
				'shares_count' => $row['shares']['count'],
				'id' =>  $row['id'],
				'user_id' =>  $row['from']['id'],
				);
		}

		if(strlen($feeds['paging']['next']) > 0 && $number < $this->limit_pages){
			$this->get_feed(str_replace('https://graph.facebook.com/', '', $feeds['paging']['next']), $number+1);
		}
		
		return $this->all_feeds;
	}



	function get_user($type = 'likes'){
		global $db;
		
		foreach($this->all_posts as $row){

			try {
				$url = $row['id'] . '/' . $type;
				$likes = $db->get_data('fb', $url);
				
				if($likes == false){
					$likes = $this->facebook->api($url);
					$db->save_data('fb', $url, $likes);
				}
				
			 } catch (FacebookApiException $e) {
				error_log($e);
				print_r($e);
			}
						
			foreach($likes['data'] as $row){
				if($type == 'likes')
					$user_ids[] = $row['id'];
				else if($type == 'comments')
					$user_ids[] = $row['from']['id'];
				else if($type == 'feed')
					$user_ids[] = $row['from']['id'];

			}
		}
		
		return $user_ids;
	}

	function get_user_genders($user_ids){
		global $db;
		
		$genders = array();
		
		$count = 0;
		foreach($user_ids as $user_id){
			
			if($count > $this->limit_users)
				break;
			
			try {
				$url = $user_id;
				$user = $db->get_data('fb', $url);

				if($user == false){
					$user = $this->facebook->api($url);
					$db->save_data('fb', $url, $user);
				}
				
			 } catch (FacebookApiException $e) {
				error_log($e);
				print_r($e);
			}

			$genders[$user['gender']]++;
			$count++;
		}
		
		return $genders;
	}


}

?>